package com.linwen.excise.leetcode;

import java.util.Arrays;
import java.util.HashSet;

/**
 * @author : linwen
 * @date : 下午5:57 2021/4/4
 */
public class _217存在重复元素 {
    public static void main(String[] args) {
        int[] arr = new int[]{1,2,3,4,5,1};
        System.out.println(_217存在重复元素.containsDuplicate(arr));
        System.out.println(_217存在重复元素.containsDuplicate2(arr));
    }
    public static boolean containsDuplicate(int[] nums) {
        HashSet<Object> set = new HashSet<>(nums.length);
        for (int i :nums) {
            if(!set.add(i)){
                return true;
            }
        }
        return false;
    }

    public static boolean containsDuplicate2(int[] nums) {
        Arrays.sort(nums);
        for (int i = 0; i < nums.length-1; i++) {
            if(nums[i]==nums[i+1]){
                return true;
            }
        }
        return false;
    }
}
